
var Observer = require('./observe');
var paramHandler = {

  merge: function () {
    var arrs = Array.prototype.slice.call(arguments);
    var fix_params = arrs[0];
    var auto_params = arrs[1];
    var out_params = arrs[2] || {};
    if (fix_params.call == 'kudu') {
      var conds = auto_params.conds[0];   		//[{ "k": "trade_day", "op": "=", "v": "2019-03-25"}]
      for (var index = 0; index < conds.length; index++) {
        var item = conds[index];       				//{ "k": "trade_day", "op": "=", "v": "2019-03-25"}
        for (var key in out_params) {  				//{ "trade_day": "2019-03-25", "dt_code": "000551", "sec_name": "创元科技"}
          if (out_params.hasOwnProperty(key)) {
            var v = out_params[key];
            if (item['k'] == key) {
              item['v'] = v
            }
          }
        }
      }
    } else if (fix_params.call == 'taf') {
      for (var key in auto_params) {
        if (auto_params.hasOwnProperty(key)) {
          for (var j in out_params) {
            if (out_params.hasOwnProperty(j)) {
              var v = out_params[j];
              if (key == j) {
                auto_params[key] = v;
              }
            }
          }
        }
      }
      //auto_params = Object.assign(auto_params, out_params);
    }
    // 添加上下文的参数到原子
    if (Observer.params.length > 0) {
      if (fix_params.call == 'kudu') {
        var conds = auto_params.conds[0];
        for (var index = 0; index < conds.length; index++) {
          var item = conds[index];
          for (var i = 0; i < Observer.params.length; i++) {
            var p = Observer.params[i];
            if (item['k'] == p['k']) {
              item['v'] = p['v'];
              item['op'] = p['op'];
            }
          }
        }
      } else if (fix_params.call == 'taf') {
        for (var key in auto_params) {
          if (auto_params.hasOwnProperty(key)) {
            for (var i = 0; i < Observer.params.length; i++) {
              var p = Observer.params[i];
              if (key == p['k']) {
                auto_params[key] = p['v'];
              }
            }
          }
        }
      }
    }
    var ret_params = Object.assign({}, fix_params, auto_params);
    return ret_params;
  }
}

module.exports = paramHandler;